<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\StoreOrderService;
use App\Services\StoreGoodsService;
use App\Services\DoctorGoodsCashService;
use App\Services\DoctorListService;
use App\Services\DoctorCashRecordService;

class StoreOrderController extends CommonController{
	private $storeOrderService;
    public function __construct(Request $request){
    	$this->storeOrderService = new StoreOrderService();
    	$this->storeGoodsService = new StoreGoodsService();
    	$this->doctorGoodsCashService = new DoctorGoodsCashService();
    	$this->doctorListService = new DoctorListService();
    	$this->doctorCashRecordService = new DoctorCashRecordService();
        parent::__construct($request); 
    }
    
    /**
     * 管理员列表
     */
    function index(){
    	$order_num = $this->request->input('order_num');
    	$status = $this->request->input('status');
    	$start_time = $this->request->input('start_time');
    	$end_time   = $this->request->input('end_time');
    	
    	$where = array();
    	if(isset($order_num) && $order_num !=''){
    		$where[] = array('order_num','like','%'.$order_num.'%');
    	}
    	
    	if(isset($status) && $status !==''){
    		$where[] = array('status','=',$status);
    	}
  		
  		if(isset($start_time) && $start_time !=''){
    		$where[] = array('created_at','>=',$start_time);
    	}
    	
    	if(isset($end_time) && $end_time !=''){
    		$where[] = array('created_at','<=',$end_time);
    	}
    	
		$result = $this->storeOrderService->paginateInfo($where,$this->pageSize);
    	return view('store.order.index',['data'=>$result]);
    }
    
    
    /*
     * 删除
     */
    function delete(){
    	$id = $this->request->input('id');
    	$where = [['id','=',$id]];
    	$result = $this->storeOrderService->deleteInfo($where);
    	return response()->json($result);
    }
    
    /**
     * 查看详情
     */
    function detail(){
    	$id = $this->request->input('id');
    	$result = $this->storeOrderService->findInfo(array('id'=>$id));
    	return view('store.order.detail',['info'=>$result['data']]);
    }
    
    /**
     *更改状态
     */
    function status(){
    	$id = $this->request->input('id');
    	$where = [['id','=',$id]];
    	$param['status'] = $this->request->input('status');
    	$result = $this->storeOrderService->updateInfo($where,$param);
    	return response()->json($result);
    }
    
    /*
     * 上传报告
     */
    function upload(){
    	$file = $this->request->file('file');
        $uploadDir = "book" . '/' .date('Y-m-d');
        $path = $file->store($uploadDir, 'public');
     	if($path){
     		$oid = $this->request->input('oid');
     		$this->storeOrderService->updateInfo(array('id'=>$oid),array('status'=>3));
     		$path = '/storage/'.$path;
    		return response()->json(['code'=>1,'msg'=>'上传成功','data'=>$path]);
     	}else{
     		return response()->json(['code'=>0,'msg'=>'上传失败']);
     	}
    }
    
    /**
     *分配佣金 
     */
    function alloteCash(int $oid){
    	$orderResult = $this->storeOrderService->findInfo(array('id'=>$oid));
    	$orderInfo = $orderResult['data'];
    	$doctor_id = $orderInfo['doctor_id'];
    	if($doctor_id != 0){//存在医生
    		$goods_list = json_decode($orderInfo->goods_list,true);
	    	foreach($goods_list as $goods){
	    		
	    		$goodsCashResult = $this->doctorGoodsCashService->findInfo(array('doctor_id'=>$doctor_id,'goods_id'=>$goods['gid']));
	    		if($goodsCashResult['code'] == 1){//存在特例佣金
	    			$cash_money = $goodsCashResult['data']['cash'];
	    		}else{
	    			$goodsResult = $this->storeGoodsService->findInfo(array('id'=>$goods['gid']));
	    			$cash_money  = $goodsResult['data']['standard_cash'];
	    		}
	    		
	    		if($orderInfo['min'] != 0){//存在优惠金额
	    			$tem_money = $cash_money - $orderInfo['min'];
	    			$cash_money = $tem_money < 0 ? 0 : $tem_money;
	    		}
	    		
	    		$result = $this->doctorListService->updateSql("update xk_doctor_list set total_fee = total_fee + {$cash_money},left_fee = left_fee + {$cash_money} where id = {$doctor_id}");
	    		if($result['code'] == 1){
	    			$row = array(
	    				'doctor_id'=>$doctor_id,
	    				'money'=>$cash_money,
	    				'type'=>0
	    			);
	    			$this->doctorCashRecordService->addInfo($row);
	    		}
	    		
	    	}
    	}
    	
    }
  
}
